<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/25
 * Time: 11:27
 */

namespace api\storage\controller;


use api\storage\model\SeatModel;
use api\storage\service\SeatService;
use api\storage\service\SocketService;

class SeatController extends AppBaseController
{

    /**
     * @api {GET} /api/storage/seat 发送灯的信息
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index
     * @apiGroup  seat
     * @apiVersion 0.0.1
     * @apiDescription 根据seatCode,找到对应的seatId，再发送灯的指令
     *
     * @apiParam {string} seatCode 站位
     * @apiParam {int} state 状态 ：1亮黄灯 0 灭灯
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiError  Error
     **/
    public function index()
    {
        $seatCode = input('seatCode');
        $status = intval(input('state'));
        $seatModel = new SeatModel();
        $seatId = $seatModel->findSeatIdByCodeAndStorageUuid($seatCode, $this->getStorageUuid());
        if (empty($seatId)) {
            $this->error('该站位没有对应的seatId');
        }
        switch ($status) {
            case 0:
                SocketService::send($seatId, $this->getStorageUuid(), SocketService::TURN_OFF_LIGHT);
                break;
            case 1:
                SocketService::send($seatId, $this->getStorageUuid(), SocketService::TURN_ON_YELLOW);
                break;
            default:
        }
        $this->success("请求成功");
    }

    /**
     * @api {PUT} /api/storage/seat/seat_bind 绑定一块设备
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  bindSeat
     * @apiGroup  seat
     * @apiVersion 0.0.1
     * @apiDescription 根据起始站位和终止站位，再结合设备码。绑定对应站位的seatId
     *
     * @apiParam {string} startCode 起始站位（二维码）
     * @apiParam {string} endCode 终止站位
     * @apiParam {string} equipmentCode 设备码（机器码）
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiError  Error
     **/
    public function bindSeat()
    {
        $startCode = input('startCode');
        $endCode = input('endCode');
        $equipmentCode = input('equipmentCode');

        $start = substr($startCode, 0, 5);
        $end = substr($endCode, 0, 5);
        if ($start != $end) {
            $this->error('请扫描同一层站位');
        }
        //如果起始的的站位大于终止站位，将头尾扫描的位置互换
        if ($startCode > $endCode) {
            $tmp = $endCode;
            $endCode = $startCode;
            $startCode = $tmp;
        }
        $seatService = new SeatService();
        $status = $seatService->bindSeat($startCode, $endCode, $equipmentCode, $this->getStorageUuid());
        switch ($status) {
            case -1:
                $this->error('绑定站位的数量超过一块设备（28个）的数量');
                break;
            case 0:
                $this->error('绑定失败');
                break;
            case 1:
                $this->success('绑定成功');
                break;
            default:
                $this->error('未知错误');
        }
    }

    /**
     * @api {PUT} /api/storage/seat/multi_seat_bind 绑定多块设备
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  bindMultiSeat
     * @apiGroup  seat
     * @apiVersion 0.0.1
     * @apiDescription 根据起始站位和终止站位，再结合设备码。绑定对应站位的seatId
     *
     * @apiParam {string} startCode 起始站位
     * @apiParam {string} endCode 终止站位
     * @apiParam {Array} equipmentCodes 多个设备码
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiError  Error
     **/
    public function bindMultiSeat()
    {
        $startCode = input('startCode');
        $endCode = input('endCode');
        $equipmentCodes = input('equipmentCodes');

        $start = substr($startCode, 0, 5);
        $end = substr($endCode, 0, 5);
        if ($start != $end) {
            $this->error('请扫描同一层站位');
        }
        //如果起始的的站位大于终止站位，将头尾扫描的位置互换
        if ($startCode > $endCode) {
            $tmp = $endCode;
            $endCode = $startCode;
            $startCode = $tmp;
        }
        $seatService = new SeatService();
        $status = $seatService->bindMultiSeat($startCode, $endCode, $equipmentCodes, $this->getStorageUuid());
        switch ($status) {
            case -1:
                $this->error('绑定站位的数量超过设备具有的数量');
                break;
            case 0:
                $this->error('绑定失败');
                break;
            case 1:
                $this->success('绑定成功');
                break;
            default:
                $this->error('未知错误');
        }
    }
}